const AllHttp=require("../../utils/AllHttp");
Page({
  data: {
    CartRes:[],
    allChecked:"",
    sum:0
  },
  onLoad:async function (options) {
    var CartRes=await AllHttp.getCart();
    var allChecked=CartRes.data.every(item=>{
      return item.isSelected;
    })
    this.setData({
      CartRes:CartRes.data,
      allChecked
    })
    this.handleSum()
  },
  /* 价格变动 */
  handleNum(event){
    var CartRes=this.data.CartRes;
    var {aid}=event.currentTarget.dataset;
    CartRes.forEach(item=>{
      if(item.id==aid){
        item.productCount=event.detail;
      }
    })
    this.setData({CartRes});
    this.handleSum()
  },
  /* 复选框 */
  handleSelect(event){
    var CartRes=this.data.CartRes;
    var {aid}=event.currentTarget.dataset;
    CartRes.forEach(item=>{
      if(item.id==aid){
        item.isSelected=event.detail;
      }
    })
    var allChecked=CartRes.every(item=>{
      return item.isSelected;
    })
    this.setData({CartRes,allChecked});
    this.handleSum()
  },
  /* 价钱 */
  handleSum(){
    var sum=0;
    var CartRes=this.data.CartRes;
    CartRes.forEach(item=>{
      if(item.isSelected){
        sum+=(item.productCount)*(item.productPrice)
      }
    })
    this.setData({
      sum:sum*100
    })
  },
  /* 全选 */
  handleAll(event){
    console.log(event.detail);
    var CartRes=this.data.CartRes;
    CartRes.forEach(item=>{
      item.isSelected=event.detail;
    })
    this.setData({
     allChecked:event.detail,
      CartRes
    })
    this.handleSum()
  },

})